package org.xlp.db.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.xlp.utils.net.XLPHttpRequestUtil;

/**
 * 解析https协议sql配置文件处理器
 * @author xlp
 * @version 1.0
 */
public class HTTPsUrlSQLXmlFileHandler extends AbstractHTTPUrlSQLXmlFileHandler{
    public HTTPsUrlSQLXmlFileHandler() {}

    /**
     * 更据协议获取xml文档
     *
     * @param sqlXmlFilePath
     * @return
     * @throws SQLXmlFileParseException 假如解析异常，则抛出该异常
     */
    @Override
    protected Document toDocument(String sqlXmlFilePath) {
        try {
            return DocumentHelper.parseText(XLPHttpRequestUtil.httpsGetRequest(sqlXmlFilePath, true));
        } catch (DocumentException e){
            throw new SQLXmlFileParseException(e);
        } catch (Exception e) {
            try {
                return DocumentHelper.parseText(XLPHttpRequestUtil.httpsPostRequest(sqlXmlFilePath, true));
            } catch (DocumentException documentException) {
                throw new SQLXmlFileParseException(e);
            }
        }
    }

    /**
     * 判断给定的路径是否符合该sql配置文件解析器，进行解析
     *
     * @param sqlXmlFilePath
     * @return 返回 true：该解析器进行解析，false：则不进行解析，交给下一个解析器去解析
     * @see AbstractSQLXmlFileHandler#handle(String, ISQLXmlFileHandlerChain)
     */
    @Override
    protected boolean match(String sqlXmlFilePath) {
        return super.match(sqlXmlFilePath)
                && sqlXmlFilePath.startsWith(FilePathPrefixConstant.HTTPS_PATH_PREFIX);
    }
}
